<?php
/** Copyright 2014 TinPhp Systems, Inc.
 * @author zhangxilian 2013-8-15
 */
if (!defined('IN_TINPHP')) {
    exit('Access Denied');
}

require_once(TIN_APP_PATH . 'include/TinAppController.php');
require_once(TIN_APP_PATH . 'include/Auth.php');
require_once(TIN_APP_PATH . 'include/utils/page.php');
/** 后台管理APP基类 */
class AdminAppController extends TinAppController {
	
	/** 开启登录验证 */
	protected $chkLogin = true;
	protected $chkLoginType = 'html';	//未登录返回的格式
	protected $adminInfo;	//用于保存当前登录用户的基本数据
	protected $powerArr;	//用户权限组
	
	public function __construct() {
        parent::__construct();
        
		$system = array();
	    $system['skin'] = $this->adminInfo['SKIN_NAME'] ? $this->adminInfo['SKIN_NAME'] : "skin_blue";
	    T::$tpl->set('system',$system);  

        if($this->chkLogin==true){
        	$this->chkAdminLogin($this->chkLoginType);
        	if(!$this->chkAdminPermissions()){
	        	exit();
	        }
        }
	}
	protected function saveDbLogs($code, $logstxt){
    	$data = array();
 		$data['type'] = $code;
 		$data['logs'] = $logstxt;
 		$this->Logic('LogsLogic')->addLogs($data);
    }
	/** 验证管理员登录，
	 * @param $returnType 返回类型，html:直接跳转，json:返回Json数据
	 * */
	public function chkAdminLogin($returnType='html'){
		$this->adminInfo = Auth::getAdminInfo();
		T::$tpl->set('admin',$this->adminInfo);
		if($this->adminInfo==null){
			$bakurl = urlencode($_SERVER["REQUEST_URI"]);
			if($returnType=='json'){
				$r = array();
				$r['code']='notlogin';
				$r['msg']='对不起！您还没有登录';
				$r['tourl']=''.urldecode($bakurl);
				echo json_encode($r);
			}else{
				echo '<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>确认登录</title></head><body>将要转向到登录界面，请稍后！
<br/>如果您的浏览器不支持自动转向<a href="'.$bakurl.'" target="_top">请点击这里</a>
<script type="text/javascript">alert("对不起！您还没有登录，\n或登录超时，请重新登录。");
window.open("admin.php?ctl=login","_top");</script></body></html>';	//encodeURIComponent(this.location.href)&tourl='.$bakurl.'
			}
            return;
		}
	}
	/** 自动拦截 验证操作权限 */
	public function chkAdminPermissions(){
		$perKey = T::$controller;
		$perAct = T::$action;
		$this->powerArr = Auth::getAdminPermission();
		if(!$this->powerArr['menuids']){
			$this->powerArr = $this->getPermission();
		}
		$nowCtl = $this->powerArr['functions'][$perKey];
	/*	var_dump($this->powerArr);
	var_dump($nowCtl);
		echo '<hr>'.$perKey.' - '.$perAct.'<br>';*/		
		if($perKey!='index'){
			if($nowCtl && in_array($perAct,$nowCtl)){
				//echo '有';
			}else{
				return $this->sysMessage('无操作权限。','',0);
			}
		}
		return true;
	}
	
	public function getPermission(){
		$role_id = $this->adminInfo['ROLE_ID'];
 		if($role_id=="1"){//如果是管理员 获取全部权限
 			$permissionArr = $this->Logic('MenuLogic')->getAllMenuFun();

 		}else{
 			$permissionArr = $this->Logic('AccountLogic')->getPermissions('menu_id,function_ids','role_id='.$role_id);
 		}
 		//print_r($permissionArr);
		//---加载菜单---
		$menuLogic = $this->Logic('MenuLogic');
//		$mainMenus = $menuLogic->getMenuAll('*','STATE=1 AND parent_id=0 AND ID in('.$permissionArr['menuids'].')','Order by order_num');
//		$tpDatas['topmenus'] = $mainMenus;
		//功能权限
		$functions=array();
		//$functions['index'] = array('default','left','right');
		$functionList = $menuLogic->getMenuFuncAll('*','menu_id in ('.$permissionArr['menuids'].')');
		foreach($functionList as $k=>$v){
			$functions[$v['CTL']][] = $v['ACT'];
		}
		$permissionArr['functions'] = $functions;

		Auth::setAdminPermission($permissionArr);
		return $permissionArr;
	}
	
	
}//End Class
?>
